<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Redis 集群 | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/60.dda416bc.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Redis 集群</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">Redis 集群</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>4/20/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Redis</span></i></div></div> <div class="theme-reco-content content__default"><blockquote><p><code>Redis</code> 集群是 Redis 提供的分布式数据库方案，集群通过 <code>分片</code>(sharding) 来进行数据共享,并提供了 复制 和 故障转移的功能</p> <p>Redis 集群 是无中心的</p></blockquote> <p><strong>不同模式的解决了什么问题</strong></p> <ul><li>主从复制：实现读写分离，提高服务的可用性，实现灾备，当主节点故障时，可以进行手动切换节点。</li> <li>主从 + 哨兵：在实现读写分离的情况下，通过 哨兵 可以实现 节点故障转移</li> <li>集群：在解决了读写分离、故障转移同时
<ul><li>通过 分片 将不同的数据，存储在不同的节点上，解决了 主从 + 哨兵情况下的单机存储达到瓶颈的情况，并且 让 Redis 具备了集群扩容的能力</li> <li>各个节点内部自己存储的集群信息，减少 哨兵集群的使用，节省了一定资源</li></ul></li></ul> <h2 id="节点的数据结构"><a href="#节点的数据结构" class="header-anchor">#</a> 节点的数据结构</h2> <blockquote><p>一 个 节点就 是一个 运 行 在 集群模式 下 的 Redis 服务器， Redis 服务器在启动时会根据 <code>cluster - enabled</code> 配置选项是否为 <code>yes</code>来决定 是否  开启服务器的 集群模式, 并会创建响应的数据结构</p></blockquote> <ul><li><strong>clusterNode</strong> ：每个节点都会创建一个，用于保存节点的基本信息</li> <li><strong>clusterLink</strong>： 保存 当前节点 锁连接的其他节点的 套接字 、输入/输出缓存区</li> <li><strong>clusterState</strong>：记录了在当前节点的视角下,集群中的节点目前所处的状态</li></ul> <img src="/oliver-vuepress/middleware/redislearn/image-20220418225925088.png" alt="foo"> <h2 id="节点的启动过程"><a href="#节点的启动过程" class="header-anchor">#</a> 节点的启动过程</h2> <p>​	 节点注册的命令:  <code>CLUSTER MEET &lt;ip&gt; &lt;port&gt;</code></p> <img src="/oliver-vuepress/middleware/redislearn/image-20220418231625953.png" alt="foo"> <p><strong>通过向<code>Node1</code>节点  发 送 CLUSTER MEET 命令，客户端可以让接收命令的  <code>Node1</code>节点  将另一个  <code>Node2</code>节点添加到 <code>Node1</code>节点  当前所在的集群里面：</strong></p> <ol><li><strong>Node1</strong>  会为 <strong>Node2</strong> 创建一个 <code>clusterNode</code> 存入 自己的 <code>clusterState.nodes</code></li> <li>Node1 向 Node2 发送一个  <code>CLUSTER MEET</code> ， 让 Node2 也创建 一个 <code>clusterNode</code> 存入 自己的 <code>clusterState.nodes</code></li> <li><strong>Node2</strong> 向 <strong>Node1</strong> 返回一个 <code>PONG</code>消息</li> <li>如何接送顺利，则 Node1 会返回 Node2 一条 PING 消息</li> <li>两个节点都获取到相应的响应信息 则任务服务器牵手成功</li></ol> <h2 id="槽指派"><a href="#槽指派" class="header-anchor">#</a> 槽指派</h2> <blockquote><p>Redis集群通过分片的方式来保存数据库中的键值对， 整个集群分为  <strong>16384</strong> 个槽（slot）</p> <p>只有对 16384 个槽都指派了，集群就会进入上线状态</p></blockquote> <ul><li>clusterNode.slots： 记录当前节点负责 的 槽 ： [00101011, 00010101, ..., 11010111] ；
<ul><li>二进制位的值为1，则为节点负责的槽</li> <li>二进制位的值为1，则为节点不负责的槽</li></ul></li></ul> <img src="/oliver-vuepress/middleware/redislearn/image-20220418232410198.png" alt="foo"> <ul><li>clusterState.slots ：记录集群全局 的槽指派信息： [node3, node2, node3, ..., node1]
<ul><li>通过全局的操作指派信息，每个节点都能知道 不是我负责的槽 需要指派给 那个节点去处理了</li></ul></li></ul> <img src="/oliver-vuepress/middleware/redislearn/image-20220418232634292.png" alt="foo"> <p><strong>为节点指派槽</strong>：<code>CLUSTER ADDSLOTS &lt;slot&gt; [slot ...]</code></p> <h2 id="集群中的命令执行"><a href="#集群中的命令执行" class="header-anchor">#</a> 集群中的命令执行</h2> <img src="/oliver-vuepress/middleware/redislearn/image-20220418233306347.png" alt="foo"> <p><strong>MOVED 错误</strong></p> <p>命令执行的过程中，如果不是该节点 负责该槽的操作的话，节点会向客户端返回 MOVED错误</p> <p>客户端会根据 MOVED 错误 返回的 ip 和 端口号，转向 该节点去执行响应的命令 （Redis-cli 会自通过 MOVED 错误 进行转换到相应的节点，我们用户端是看不到节点返回的1 MOVED错误）</p> <h2 id="集群中的过期元素"><a href="#集群中的过期元素" class="header-anchor">#</a> 集群中的过期元素</h2> <blockquote><p>在 clusterState 中使用  .slots_to_keys 跳跃表来保存槽 和 键值的关系</p></blockquote> <ul><li>新增数据时：会在跳跃表中创建一个元素，score：是槽位。 value 存放键</li> <li>节点元素过期时:  可以通过 slots_to_keys 进行确认需要删除元素的位置</li></ul> <p>也可以通过  <code>slots_to_keys</code> 完成 统计出 槽slot 中存放了多少个键</p> <h2 id="重新分片"><a href="#重新分片" class="header-anchor">#</a> 重新分片</h2> <blockquote><p><code>重新分片</code> Redis 支持将已经分配的槽，重新指派给某个节点，并且相关槽上的键值对 也会被转移到新的节点上。</p> <p>这个 <code>重新分片</code> 的过程 可以在不下线的情况下进行</p></blockquote> <p>Redis 使用 Redis扩展： <code>Redis-trib</code> 来完成重新分片的过程</p> <p>实现原理：</p> <ul><li>目标节点
<ul><li>会通过 <code>importing_slots_from</code> 数组 记录槽 锁对应的 <code>clusterNode</code>, 用于标识该槽点正在被迁移</li> <li>当 <code>importing_slots_from == null</code> 则标识没有分片在进行</li></ul></li> <li>源节点：
<ul><li>会通过 <code>migrating_slots_to</code> 数组 记录槽 所对应的 <code>clusterNode （）</code>, 用于标识该槽点正在被迁移</li> <li>当 <code>migrating_slots_to == null</code> 则标识没有分片在进行</li></ul></li></ul> <p><strong>ASK错误</strong></p> <blockquote><p>在重新分⽚未完成时，如果源节点接收到请求，且命令中的key已经被迁移到了⽬标节点，就会出现ASK错误。</p></blockquote> <img src="/oliver-vuepress/middleware/redislearn/image-20220419095202338.png" alt="foo"> <p><strong>ASKING命令</strong></p> <ul><li>当 客户端请求的key 在 迁移的槽中，则会返回ASK错误，同时会为客户端加上 <code>REDIS_ASKING</code> 标识</li> <li>客户端 会向 迁移节点 先发送  ASKING命令 后 在进行 key的相关操作</li> <li>客户端的   <code>REDIS_ASKING</code> 标识 是一次性的（不影响后续操作）</li></ul> <h2 id="故障转移"><a href="#故障转移" class="header-anchor">#</a> 故障转移</h2> <h3 id="复制"><a href="#复制" class="header-anchor">#</a> 复制</h3> <p><strong>设置从节点</strong></p> <p>向主节点发送 CLUSTER REPLICATE &lt;node_id&gt; 命令 设置从节点</p> <ul><li>1、从 将 clusterState.nodes字典 找到 对应的 node_id 节点  clusterState.myself.slaveof 指针 指向 主节点</li> <li>2、将自己 clusterState.myself.flags 属性 设置为  REDIS_NODE_SLAVE标识</li> <li>3、调用复制代码 进行 主从复制 相当于 是从节点 向 主节点 发送 slaveOF 命令</li></ul> <p><strong>主节点</strong></p> <p>都会通过 ClusterNode 记录，该主节点下 的从节点数量（numslaves），和 从节点地址（slaves）</p> <h3 id="故障检测"><a href="#故障检测" class="header-anchor">#</a> 故障检测</h3> <blockquote><p>主要分为2个阶段：疑似下线、正式下线<br>
判断方式：单机认为下线，先标识为 疑似下线，多数集群节点认为是下线，则为正式下线</p></blockquote> <p><strong>所有的主节点，都会相互定时发送 PING 消息，检测对⽅是否在线。并且会分享检测信息。如果半数以上的主节点都认为某节点疑似下线，那么他就会被标记为下线</strong></p> <h3 id="故障转移-2"><a href="#故障转移-2" class="header-anchor">#</a> 故障转移</h3> <p><strong>故障转移过程</strong></p> <ol><li><p>复制下线主节点的所有从节点里面，会有个从节点被选中；</p></li> <li><p>被选中的从节点会执行SLAVEOF 命令成为新的主节点；</p></li> <li><p>新的主节点会撤销所有对已下线主节点的槽指派，并将这些槽全部指派给自己</p></li> <li><p>新的主节点向集群广播一条PONE消息，高坠自己变为主节点</p></li> <li><p>新的主节点开始工作</p></li></ol> <p><strong>选举过程</strong></p> <ol><li><p>集群的配置纪元是一个自增计数器，它的初始值为0；</p></li> <li><p>当集群里的某个节点开始一次故障转移操作时，集群配置纪元的值会被增一；</p></li> <li><p>对于每个配置纪元，集群里每个负责处理槽的主节点都有一次投票的机会，而第一个向主节点要求投票的从节点将获得主
节点的投票；</p></li> <li><p>当从节点发现自己正在复制的主节点进入已下线状态时，从节点会向集群广播一条消息，要求所有收到这条消息，并且具
有投票权的主节点向这个从节点投票；</p></li> <li><p>如果一个主节点具有投票权，并且这个主节点尚未投票给其他从节点，那么主节点将向要求投票的从节点返回一条消息，
标识这个主节点支持从节点成为新的主节点；</p></li> <li><p>每个参与选举的从节点都会接收消息，并根据自己收到了多少条这种消息来统计自己获得了多少主节点的支持；</p></li> <li><p>如果集群里有N个具有投票权的主节点，那么当一个从节点收集到大于等于N/2 + 1张支持票时，这个从节点就会当选为新
的主节点；</p></li> <li><p>因为在每个配置纪元里面，每个具有投票权的主节点只能投一次票，所以如果有N个主节点进行投票，那么具有大于等于
N/2 + 1张支持票的从节点只会有一个，这确保了新的主节点只会有一个。</p></li> <li><p>如果再一个配置及原理没有从节点能收集到足够多的支持票，那么集群进入一个新的配置及原，并再次进行选举，直到选
出新的主节点为止。</p></li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#节点的数据结构" class="sidebar-link reco-side-节点的数据结构" data-v-cb1513f6>节点的数据结构</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#节点的启动过程" class="sidebar-link reco-side-节点的启动过程" data-v-cb1513f6>节点的启动过程</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#槽指派" class="sidebar-link reco-side-槽指派" data-v-cb1513f6>槽指派</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#集群中的命令执行" class="sidebar-link reco-side-集群中的命令执行" data-v-cb1513f6>集群中的命令执行</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#集群中的过期元素" class="sidebar-link reco-side-集群中的过期元素" data-v-cb1513f6>集群中的过期元素</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#重新分片" class="sidebar-link reco-side-重新分片" data-v-cb1513f6>重新分片</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#故障转移" class="sidebar-link reco-side-故障转移" data-v-cb1513f6>故障转移</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#复制" class="sidebar-link reco-side-复制" data-v-cb1513f6>复制</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#故障检测" class="sidebar-link reco-side-故障检测" data-v-cb1513f6>故障检测</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Redis%E9%9B%86%E7%BE%A4.html#故障转移-2" class="sidebar-link reco-side-故障转移-2" data-v-cb1513f6>故障转移</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/60.dda416bc.js" defer></script>
  </body>
</html>
